package com.caigou.controller;

import com.caigou.bean.cms.entity.FriendshipLink;
import com.caigou.bean.cms.param.FriendshipLinkParam;
import com.caigou.bean.cms.param.LinkTypeParam;
import com.caigou.bean.exam.entity.Users;
import com.caigou.enums.ResponseEnum;
import com.caigou.security.utils.CurrentUser;
import com.caigou.service.FriendshipLinkService;
import com.caigou.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/link/")
public class FriendshipLinkController {
    @Resource
    private FriendshipLinkService friendshipLinkService;

    @RequestMapping("addFriendshipLink")
    @PreAuthorize("hasAnyAuthority('youqinglianjieguanli')")
    public Result addFriendshipLink(@RequestBody FriendshipLinkParam friendshipLinkParam, @CurrentUser Users user){
        if(friendshipLinkParam.getLinkName()!=null && friendshipLinkParam.getLinkName().length()>32){
            return Result.error("友链名称不能超过32个字", "友链名称不能超过32个字");
        }
        if(friendshipLinkParam.getLinkUrl()!=null && friendshipLinkParam.getLinkUrl().length()>128){
            return Result.error("友链跳转链接不能超过128个字", "友链跳转链接不能超过128个字");
        }
        return friendshipLinkService.saveFriendshipLink(friendshipLinkParam);
    }

    @RequestMapping("updateFriendshipLink")
    @PreAuthorize("hasAnyAuthority('youqinglianjieguanli')")
    public Result updateFriendshipLink(@RequestBody FriendshipLinkParam friendshipLinkParam, @CurrentUser Users user){
        if(friendshipLinkParam.getLinkName()!=null && friendshipLinkParam.getLinkName().length()>32){
            return Result.error("友链名称不能超过32个字", "友链名称不能超过32个字");
        }
        if(friendshipLinkParam.getLinkUrl()!=null && friendshipLinkParam.getLinkUrl().length()>128){
            return Result.error("友链跳转链接不能超过128个字", "友链跳转链接不能超过128个字");
        }
        return friendshipLinkService.updateFriendshipLink(friendshipLinkParam);
    }

    @RequestMapping("deleteFriendshipLink")
    @PreAuthorize("hasAnyAuthority('youqinglianjieguanli')")
    public Result deleteFriendshipLink(@RequestBody FriendshipLinkParam friendshipLinkParam, @CurrentUser Users user){
        return friendshipLinkService.deleteFriendshipLink(friendshipLinkParam);
    }

    @RequestMapping("listFriendshipLink")
    @PreAuthorize("hasAnyAuthority('youqinglianjieguanli')")
    public Result listFriendshipLink(@CurrentUser Users user){
        List<FriendshipLink> list = friendshipLinkService.listFriendshipLink();
        return Result.success(list);
    }

    @RequestMapping("moveFriendshipLink")
    @PreAuthorize("hasAnyAuthority('youqinglianjieguanli')")
    public Result moveFriendshipLink(@RequestBody FriendshipLinkParam param, @CurrentUser Users user){
        return friendshipLinkService.moveFriendshipLink(param);
    }

    @RequestMapping("publishFriendshipLink")
    @PreAuthorize("hasAnyAuthority('youqinglianjieguanli')")
    public Result publishFriendshipLink(@CurrentUser Users user){
        return friendshipLinkService.publishFriendshipLink();
    }

    @RequestMapping("addLinkType")
    @PreAuthorize("hasAnyAuthority('youqinglianjieguanli')")
    public Result addLinkType(@RequestBody LinkTypeParam param, @CurrentUser Users user){
        if(param.getTypeName()==null){
            return Result.error("友链类型名称不能为空", "友链类型名称不能为空");
        }
        if(param.getTypeName().length()>8){
            return Result.error("友链类型长度不能超过8个字", "友链类型长度不能超过8个字");
        }
        return friendshipLinkService.saveLinkType(param);
    }

    @RequestMapping("deleteLinkType")
    @PreAuthorize("hasAnyAuthority('youqinglianjieguanli')")
    public Result deleteLinkType(@RequestBody LinkTypeParam param, @CurrentUser Users user){
        return friendshipLinkService.deleteLinkType(param);
    }

    @RequestMapping("listLinkType")
    @PreAuthorize("hasAnyAuthority('youqinglianjieguanli')")
    public Result listLinkType(@CurrentUser Users user){
        return friendshipLinkService.listLinkType(null);
    }

    @RequestMapping("updateLinkType")
    @PreAuthorize("hasAnyAuthority('youqinglianjieguanli')")
    public Result updateLinkType(@RequestBody LinkTypeParam param, @CurrentUser Users user){
        return friendshipLinkService.updateLinkType(param);
    }
}
